﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace CY.GFiveWeb.MessageManagement
{
    public partial class MessageAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            CY.GFive.Core.Business.Web_Messages message = new CY.GFive.Core.Business.Web_Messages();
            CY.GFive.Core.Business.Web_UserMessage usermessage;

            message.Message_Title = MessageTitle.Text;
            message.Message_Content = Server.HtmlEncode(MessageContent.Value.Trim());
            message.Message_PubDate = System.DateTime.Now;
            message.Manager_Code = Session["ManagerCode"].ToString();
            if (ddl_IsPub.SelectedValue == "")
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "error", "<script>alert('是否为公告不能为空!')</script>");
                return;
            }
            message.Manager_IsPub = int.Parse(ddl_IsPub.SelectedValue.ToString());
            message.Save();

            if (ddl_IsPub.SelectedValue.Equals("0"))
            {
                if (tb_UserIDs.Text.Trim().Equals(""))
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "AddFailed!", "<script>alert('消息发送对象不能为空!');</script>");
                    return;
                }
                string[] userIdList = tb_UserIDs.Text.Split(',');

                foreach (string item in userIdList)
                {
                    usermessage = new CY.GFive.Core.Business.Web_UserMessage { UserID = int.Parse(item), MessageID = message.Id, UserMessageState = 0 };
                    message.Users.AddOnSave(usermessage);
                }
            }
            else
            {
                IList<CY.GFive.Core.Business.StaffInfo> userlist = CY.GFive.Core.Business.StaffInfo.GetAllUsers();

                foreach (CY.GFive.Core.Business.StaffInfo u in userlist)
                {
                    usermessage = new CY.GFive.Core.Business.Web_UserMessage { User_ID = u.ID, Message_ID = message.Id, UserMessage_State = 0 };
                    message.Users.AddOnSave(usermessage);
                }
            }
            message.Save();
            CY.GFive.Core.Business.Log log = new CY.GFive.Core.Business.Log();
            log.Manager =Manager.Code;
            log.Message =Manager.Code+"添加消息：" + message.Title;
            log.Save();
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "短信息成功保存!", "<script>alert('保存成功!');window.location.href='MessageList.aspx'</script>");
        }
    }
}
